
//01:
import { createSlice, configureStore, combineReducers } from '@reduxjs/toolkit';

//导入持久化插件中的两个方法
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
//02: 创建切片
const userSlice = createSlice({
    name: 'user',
    initialState: {
        userid: '100',
    },
    reducers: {
        update: (state, action) => {
            state.userid = action.payload
        }
    }
})

export const rootReducer = combineReducers({
    counter: userSlice.reducer
})

const persistConfig = {
    key: 'root',
    storage,
    blacklist: []
}

const myPersistReducer = persistReducer(persistConfig, rootReducer)
// 03: 导出修改state 的方法
export const { update } = userSlice.actions

// 04: 创建仓库
export const store = configureStore({
    reducer: myPersistReducer,
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({
            serializableCheck: false,
        })
})

// 05: 
export const persistor = persistStore(store)